import re


from django.utils.safestring import mark_safe


class MyPagenation:

    def __init__(self,page_num,total_count,base_url,get_data=None,per_page_num=10,page_num_show=5):


        self.per_page_num = per_page_num  # 每页显示10条
        self.get_data = get_data           #搜索条件
        # 页面生成页码的数量
        self.page_num_show = page_num_show  # 4 5 6 7 8
        self.base_url = base_url

        try:
            page_num = int(page_num)
        except Exception:
            page_num = 1

        self.page_num = page_num

        # 商和余数
        a, b = divmod(total_count, self.per_page_num)
        # 总页码数
        if b:
            page_num_count = a + 1
        else:
            page_num_count = a

        self.page_num_count = page_num_count

        if page_num <= 0:
            page_num = 1
        elif page_num > page_num_count:
            page_num = page_num_count

        half_show = self.page_num_show // 2  # 左右两边的个数 2

        if page_num - half_show <= 0:
            start_page_num = 1
            end_page_num = self.page_num_show + 1
        elif page_num + half_show > page_num_count:
            start_page_num = page_num_count - self.page_num_show + 1
            end_page_num = page_num_count + 1

        else:
            start_page_num = self.page_num - half_show  # 4
            end_page_num = self.page_num + half_show + 1  # 9

        #如果总数据的页数小于设定的展示页码总数 那么就显示数据的展示页数
        if page_num_count < self.page_num_show:
            start_page_num = 1
            end_page_num = self.page_num_count + 1

        self.start_page_num = start_page_num
        self.end_page_num = end_page_num

    @property
    def start_data_num(self):
        # (page_num - 1) * per_page_num
        return (self.page_num - 1) * self.per_page_num

    @property
    def end_data_num(self):
        # page_num * per_page_num
        return self.page_num * self.per_page_num


    def page_html(self):

        page_num_range = range(self.start_page_num, self.end_page_num)
        page_html = ''
        page_pre_html = '''
                    <nav aria-label="Page navigation">
                    <ul class="pagination">
                    '''

        page_html += page_pre_html
        first_page_html = f'''<li>
                                      <a href="{self.base_url}?page={1}" aria-label="Previous">
                                        <span aria-hidden="true">首页</span>
                                      </a>
                         </li>'''
        page_html += first_page_html
        if self.page_num <= 1:
            pre_page = f'''<li class="disabled">
                              <a href="javascript:void(0)" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                              </a>
                            </li>'''
        else:
            pre_page = f'''<li>
                                      <a href="{self.base_url}?page={self.page_num - 1}" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                      </a>
                         </li>'''
        page_html += pre_page

        #选中页加颜色
        for i in page_num_range:
            if i == self.page_num:
                page_html += '<li><a href="{0}?{2}page={1}">{3}</a></li>'.format(self.base_url,i,re.sub('page=\d+','',self.get_data) if 'page=' in self.get_data else self.get_data + '&',i)
            else:
                page_html += '<li><a href="{0}?{2}page={1}">{3}</a></li>'.format(self.base_url,i,re.sub('page=\d+','',self.get_data) if 'page=' in self.get_data else self.get_data + '&',i)
        if self.page_num >= self.page_num_count:
            page_next_html = f'''
                            <li class="disabled">
                                <a href="#" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        '''
        else:
            page_next_html = f'''
                                    <li >
                                        <a href="{self.base_url}?page={self.page_num + 1}" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                        </a>
                                    </li>
                                '''

        page_html += page_next_html
        last_page_html = f'''<li><a href="{self.base_url}?page={self.page_num_count}" aria-label="Previous"><span aria-hidden="true">尾页</span>
                        </a>
                        </li>'''
        page_html += last_page_html
        end_html = '</ul></nav>'
        page_html += end_html



        return mark_safe(page_html)   #识别成标签  前端就不用加safe